package gui;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.*;
import javafx.scene.control.*;
import javafx.scene.input.*;

/**
 * Creates a custom MenuBar which already contains File, Edit, View, About
 * @author Etienne Bruines
 * @author Bas Dobbelsteen
 */
public class MenuBar extends javafx.scene.control.MenuBar
{
	private final Menu file, edit, view, task, about;
	
	private final MenuItem file_new, file_open, file_separator_1, file_save, file_save_as, file_separator_2, file_close, file_exit;
	private final MenuItem edit_copy, edit_cut, edit_paste, edit_separator_1, edit_undo, edit_redo;
	private final CheckMenuItem view_ruler, view_grid, view_toolbar;
	private final MenuItem about_help, about_about, about_feedback;
	private final MenuItem start_task, end_task;
	
	private final KeyCombination file_new_keycode = new KeyCodeCombination(KeyCode.N, KeyCombination.CONTROL_DOWN);
	private final KeyCombination file_open_keycode = new KeyCodeCombination(KeyCode.O, KeyCombination.CONTROL_DOWN);
	private final KeyCombination file_save_keycode = new KeyCodeCombination(KeyCode.S, KeyCombination.CONTROL_DOWN);
	private final KeyCombination file_close_keycode = new KeyCodeCombination(KeyCode.W, KeyCombination.CONTROL_DOWN);
	private final KeyCombination file_exit_keycode = new KeyCodeCombination(KeyCode.F4, KeyCombination.ALT_DOWN);
	
	private final KeyCombination edit_copy_keycode = new KeyCodeCombination(KeyCode.C, KeyCombination.CONTROL_DOWN);
	private final KeyCombination edit_cut_keycode = new KeyCodeCombination(KeyCode.X, KeyCodeCombination.CONTROL_DOWN);
	private final KeyCombination edit_paste_keycode = new KeyCodeCombination(KeyCode.V, KeyCombination.CONTROL_DOWN);
	private final KeyCombination edit_undo_keycode = new KeyCodeCombination(KeyCode.Z, KeyCombination.CONTROL_DOWN);
	private final KeyCombination edit_redo_keycode = new KeyCodeCombination(KeyCode.Y, KeyCombination.CONTROL_DOWN);
	
	private final KeyCombination about_help_keycode = new KeyCodeCombination(KeyCode.F1);
	
	public MenuBar()
	{
		super();
		file = new Menu("File");
		file_new = new MenuItem("New...");
		file_new.setAccelerator(file_new_keycode);
		file_new.setOnAction(file_new_handler);
		file_open = new MenuItem("Open...");
		file_open.setAccelerator(file_open_keycode);
		file_open.setOnAction(file_open_handler);
		file_separator_1 = new SeparatorMenuItem();
		file_save = new MenuItem("Save");
		file_save.setAccelerator(file_save_keycode);
		file_save.setOnAction(file_save_handler);
		file_save.setDisable(true);
		file_save_as = new MenuItem("Save as...     ");
		file_save_as.setOnAction(file_save_as_handler);
		file_save_as.setDisable(true);
		file_separator_2 = new SeparatorMenuItem();
		file_close = new MenuItem("Close image");
		file_close.setAccelerator(file_close_keycode);
		file_close.setOnAction(file_close_handler);
		file_close.setDisable(true);
		file_exit = new MenuItem("Exit program");
		file_exit.setAccelerator(file_exit_keycode);
		file_exit.setOnAction(file_exit_handler);
		file.getItems().addAll(file_new, /*file_open, file_separator_1, 
				file_save, file_save_as, file_separator_2,*/ file_close, file_exit);
		
		edit = new Menu("Edit");
		edit_copy = new MenuItem("Copy");
		edit_copy.setAccelerator(edit_copy_keycode);
		edit_copy.setOnAction(edit_copy_handler);
		edit_copy.setDisable(true);
		edit_cut = new MenuItem("Cut");
		edit_cut.setAccelerator(edit_cut_keycode);
		edit_cut.setOnAction(edit_cut_handler);
		edit_cut.setDisable(true);
		edit_paste = new MenuItem("Paste");
		edit_paste.setAccelerator(edit_paste_keycode);
		edit_paste.setOnAction(edit_paste_handler);
		edit_paste.setDisable(true);
		edit_separator_1 = new SeparatorMenuItem();
		edit_undo = new MenuItem("Undo");
		edit_undo.setAccelerator(edit_undo_keycode);
		edit_undo.setOnAction(edit_undo_handler);
		edit_undo.setDisable(true);
		edit_redo = new MenuItem("Redo");
		edit_redo.setAccelerator(edit_redo_keycode);
		edit_redo.setOnAction(edit_redo_handler);
		edit_redo.setDisable(true);
		edit.getItems().addAll(edit_undo/*, edit_redo, edit_separator_1, edit_copy, edit_cut, edit_paste*/);
		
		view = new Menu("View"); // ruler, grid, toolbar
		view_ruler = new CheckMenuItem("Show ruler");
		view_ruler.setSelected(true);
		view_ruler.setOnAction(view_ruler_handler);
		view_ruler.setDisable(true);
		view_grid = new CheckMenuItem("Show grid");
		view_grid.setSelected(true);
		view_grid.setOnAction(view_grid_handler);
		view_grid.setDisable(true);
		view_toolbar = new CheckMenuItem("Show toolbar");
		view_toolbar.setSelected(true);
		view_toolbar.setOnAction(view_toolbar_handler);
		view.getItems().addAll(view_ruler, view_grid, view_toolbar);
		
		task = new Menu("Task");
		start_task = new MenuItem("Start task");
		start_task.setOnAction(start_task_handler);
		end_task = new MenuItem("End task");
		end_task.setDisable(true);
		end_task.setOnAction(end_task_handler);
		task.getItems().addAll(start_task, end_task);
		
		Main.getKTimer().startedProperty().addListener(new ChangeListener<Boolean> () {
			@Override public void changed(ObservableValue<? extends Boolean> arg0, Boolean arg1, Boolean arg2)
			{
				if (arg1)
				{
					start_task.setDisable(false);
					end_task.setDisable(true);
				}
				else
				{
					start_task.setDisable(true);
					end_task.setDisable(false);
				}
			}
		});
		
		about = new Menu("Help");
		about_help = new MenuItem("Help...");
		about_help.setAccelerator(about_help_keycode);
		about_help.setOnAction(about_help_handler);
		about_feedback = new MenuItem("Feedback...");
		about_feedback.setOnAction(about_feedback_handler);
		about_about = new MenuItem("About " + Main.getTitle() + "...");
		about_about.setOnAction(about_about_handler);
		about.getItems().addAll(about_help, about_feedback, about_about);
		
		this.getMenus().addAll(file, edit, /*view,*/ task /*about*/);
	}
	
	private final EventHandler<ActionEvent> file_new_handler = new EventHandler<ActionEvent>() {
		@Override public void handle(ActionEvent arg0) 
		{
			Main.getLogger().logLine("New File!");
			// TODO: start new file
			Main.getImageManager().open();
		}
	};

	private final EventHandler<ActionEvent> file_open_handler = new EventHandler<ActionEvent>() {
		@Override public void handle(ActionEvent arg0)
		{
			Main.getLogger().logLine("Opened File!");
			// TODO: opening file
		}
	};
	
	private final EventHandler<ActionEvent> file_save_handler = new EventHandler<ActionEvent>(){
		@Override public void handle(ActionEvent arg0)
		{
			Main.getLogger().logLine("Saved File!");
			// TODO: saving file
		}
	};
	
	private final EventHandler<ActionEvent> file_save_as_handler = new EventHandler<ActionEvent>(){
		@Override public void handle(ActionEvent arg0)
		{
			Main.getLogger().logLine("Saved File as (...)!");
			// TODO: saving file
		}
	};
	
	private final EventHandler<ActionEvent> file_close_handler = new EventHandler<ActionEvent>(){
		@Override public void handle(ActionEvent arg0)
		{
			Main.getLogger().logLine("Closed image!");
			// TODO: saving file
		}
	};
	
	private final EventHandler<ActionEvent> file_exit_handler = new EventHandler<ActionEvent>() {
		@Override public void handle(ActionEvent arg0)
		{
			Main.getLogger().logLine("Bye bye!");
			// TODO: 'are you sure'? / 'save file?'
			System.exit(0);
		}
	};
	
	private final EventHandler<ActionEvent> edit_copy_handler = new EventHandler<ActionEvent>() {
		@Override public void handle(ActionEvent arg0)
		{
			Main.getLogger().logLine("Copied!");
			// TODO: Actually copying Object / Layer
		}
	};
	
	private final EventHandler<ActionEvent> edit_cut_handler = new EventHandler<ActionEvent>() {
		@Override public void handle(ActionEvent arg0)
		{
			Main.getLogger().logLine("Cut!");
			// TODO: Cutting the Object / Layer -> moving it to working memory while removing it from array
		}
	};
	
	private final EventHandler<ActionEvent> edit_paste_handler = new EventHandler<ActionEvent>() {
		@Override public void handle(ActionEvent arg0)
		{
			Main.getLogger().logLine("Pasted!");
			// TODO: Pasting Object -> moving it from the working memory to an array
		}
	};
	
	private final EventHandler<ActionEvent> edit_undo_handler = new EventHandler<ActionEvent>() {
		@Override public void handle(ActionEvent arg0)
		{
			Main.getLogger().logLine("Undone last move.");
			// TODO: undo last move -> may be used more than once
		}
	};
	
	private final EventHandler<ActionEvent> edit_redo_handler = new EventHandler<ActionEvent>() {
		@Override public void handle(ActionEvent arg0)
		{
			Main.getLogger().logLine("Redone last undone move.");
			// TODO: redo last move -> may be used more than once, as many times as "undo" has been called
		}
	};
	
	private final EventHandler<ActionEvent> view_ruler_handler = new EventHandler<ActionEvent>() {
		@Override public void handle(ActionEvent arg0)
		{
			if (((CheckMenuItem)arg0.getSource()).isSelected())
				Main.getLogger().logLine("Changed ruler to visible");
			else
				Main.getLogger().logLine("Changed ruler to invisible");
			// TODO: View or hide the "Ruler"
		}
	};
	
	private final EventHandler<ActionEvent> view_grid_handler = new EventHandler<ActionEvent>() {
		@Override public void handle(ActionEvent arg0)
		{
			if (((CheckMenuItem)arg0.getSource()).isSelected())
				Main.getLogger().logLine("Changed grid to visible");
			else
				Main.getLogger().logLine("Changed grid to invisible");
			// TODO: View or hide the "Grid"
		}
	};
	
	private final EventHandler<ActionEvent> view_toolbar_handler = new EventHandler<ActionEvent>() {
		@Override public void handle(ActionEvent arg0)
		{
			if (((CheckMenuItem)arg0.getSource()).isSelected())
			{
				Main.getToolBar().setVisible(true);
				Main.getLogger().logLine("Changed toolbar to visible");
			}
			else
			{
				Main.getToolBar().setVisible(false);
				Main.getLogger().logLine("Changed toolbar to invisible");
			}
			// TODO: ask yourself: do we want this? Would anyone ever want to hide the toolbar?
		}
	};
	
	private final EventHandler<ActionEvent> about_help_handler = new EventHandler<ActionEvent>() {
		@Override public void handle(ActionEvent arg0)
		{
			Main.getLogger().logLine("Opened help window");
			// TODO: Opening a help window - can be used by using a "Stage"
		}
	};
	
	private final EventHandler<ActionEvent> about_feedback_handler = new EventHandler<ActionEvent>() {
		@Override public void handle(ActionEvent arg0)
		{
			Main.getLogger().logLine("Opened feedback window, where we can have a questionnaire");
			// TODO: opening a feedback window, make up questions for the feedback window, and manage them in a way
		}
	};
	
	private final EventHandler<ActionEvent> about_about_handler = new EventHandler<ActionEvent>() {
		@Override public void handle(ActionEvent arg0)
		{
			Main.getLogger().logLine("Opened about " + Main.getTitle() + " window");
			// TODO: Opening an 'about window', which shows information about this software, version, ourselves, etc.
		}
	};
	
	private final EventHandler<ActionEvent> start_task_handler = new EventHandler<ActionEvent>() {
		@Override public void handle(ActionEvent arg0)
		{
			Main.getKTimer().startTimer(0);
		}
	};
	
	private final EventHandler<ActionEvent> end_task_handler = new EventHandler<ActionEvent> () {
		@Override public void handle(ActionEvent arg0)
		{
			Main.getKTimer().stopTimer();
		}
	};
}
